/**
 *
 */
package com.yangtao.prototype.common.tree;

import com.yangtao.prototype.system.domain.MenuTree;
import com.yangtao.prototype.system.domain.Organization;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 杨涛
 */
public class CheckBoxTree implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * tree的主键
	 */
	private String id;

	/**
	 * tree的显示文字
	 */
	private String text;

	/**
	 * 是否选中
	 */
	private boolean checkbox;

	/**
	 * 树的层次
	 */
	private int level;

	/**
	 * 子节点
	 */
	private List<CheckBoxTree> children;

	public static CheckBoxTree loadFromOrganize(Organization model) {
		CheckBoxTree ret = new CheckBoxTree();
		if (model == null)
			return null;
		ret.setId(model.getId());
		ret.setText(model.getName());
		ret.setLevel(model.getLevel());
		if (model.getSubOrganization() != null && model.getSubOrganization().size() > 0) {
			ret.setChildren(new ArrayList<CheckBoxTree>());
			for (Organization subModel : model.getSubOrganization()) {
				ret.getChildren().add(loadFromOrganize(subModel));
			}
		}
		return ret;
	}

	public static CheckBoxTree loadFromMenu(MenuTree model,
	                                        Set<String> checkedModel) {
		CheckBoxTree ret = new CheckBoxTree();
		if (model == null)
			return null;
		ret.setId(model.getId());
		ret.setText(model.getName());
		ret.setLevel(model.getLevel());
		if (checkedModel.contains(model.getId())) {
			ret.setCheckbox(true);
		}
		if (model.getSubMenus() != null && model.getSubMenus().size() > 0) {
			ret.setChildren(new ArrayList<CheckBoxTree>());
			for (MenuTree subModel : model.getSubMenus()) {
				ret.getChildren().add(loadFromMenu(subModel, checkedModel));
			}
		}
		return ret;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the text
	 */
	public String getText() {
		return text;
	}

	/**
	 * @param text the text to set
	 */
	public void setText(String text) {
		this.text = text;
	}

	/**
	 * @return the children
	 */
	public List<CheckBoxTree> getChildren() {
		return children;
	}

	/**
	 * @param children the children to set
	 */
	public void setChildren(List<CheckBoxTree> children) {
		this.children = children;
	}

	/**
	 * @return the checkbox
	 */
	public boolean getCheckbox() {
		return checkbox;
	}

	/**
	 * @param checkbox the checkbox to set
	 */
	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	/**
	 * @return the level
	 */
	public int getLevel() {
		return level;
	}

	/**
	 * @param level the level to set
	 */
	public void setLevel(int level) {
		this.level = level;
	}

}
